<?php // no direct access
defined('_JEXEC') or die('Restricted access');

require_once( $dirname . DS. 'thumb.php' );

	$moduleid = $params->get('moduleid','imf');

	$dflttmpl = '<a class="highslide" href="{img}" onclick="return hs.expand(this,{slideshowGroup: \'{_group}\', captionText: \'{caption}\'})" title="{hreftitle}">'
		   .'<img id="{_id}" src="{rflct}" alt="{imgalt}" title="{imgtitle}"/></a>';

	if ($params->get('outputtemplate') != '')
	{
		//
		//	load the template file
		//
		$dflttmpl = modImageFlowHelper::getTemplate( $params->get('outputtemplate') );
	}

	$dflttmpl .= PHP_EOL;

	$hide_slider = (($params->get('imageslider') == '1') ? 'false' : 'true' );
	$hide_caption = (($params->get('captions') == '1') ? 'false' : 'true' );
	$previousnextbuttons = (($params->get('previousnextbuttons') == '1') ? 'true' : 'false' );
	$link_focused_only = $params->get('linkfocusedonly');
	$autostep = (int)$params->get('autostep',0);

	$glidetoimage = $params->get('glidetoimage','1');
	//	Have they given us a percentage?
	if (JString::substr($glidetoimage, -1) == '%')
	{
		//	Yes, remove the % sign
		$glidetoimage = (int) JString::substr($glidetoimage, 0, -1);
		$glidetoimage = (int) (count($images)) * ($glidetoimage/100);
		$glidetoimage = (int) ($glidetoimage+.50);
	}
	else
	{
		$glidetoimage = (int) $glidetoimage;
	}
	if ($glidetoimage > count($images))
	{
		$glidetoimage = count($images);
	}
	if ($glidetoimage < 1)
	{
		$glidetoimage = 1;
	}
	$glidetoimage--;

	$glidespeed = (int) $params->get('glidespeed', 50 );
	$glidespeed = max( 10, min( 1000, $glidespeed ));
	$imagestacksize = (int) $params->get('imagestacksize', 4 );
	$imagestacksize = max( min( $imagestacksize, 9 ), 1 );
	$xstep = (int) $params->get('xstep', 150 );
	$blurthumbs = (int)$params->get('blurthumbs','0');
	$blurthumbs = max(min($blurthumbs, 9), 0);
	$sharpenthumb = (int)$params->get('sharpenthumb','0');
	$sharpenthumb = max(min($sharpenthumb, 9), 0);
	$darkenthumbs = (int)$params->get('darkenthumbs','0');
	$darkenthumbs = max(min($darkenthumbs, 9), 0);
	$grayscalethumbs =(($params->get('grayscalethumbs','0') == '1') ? true : false);
	$transparent =(($params->get('alphareflect','0') == '1') ? true : false);
	$noscaling = (($params->get('imagescaling','1') == '1') ? 'false' : 'true' );
	if (!function_exists('imagefilter'))
	{
		$grayscalethumbs = false;
	}
	if (!function_exists('imageconvolution'))
	{
		$blurthumbs = 0;
		$darkenthumbs = 0;
		$sharpenthumb = 0;
	}
	$thumbeffects = (($blurthumbs > 0 || $darkenthumbs > 0 || $grayscalethumbs ) ? 'true' : 'false' );
	$borderwidth = $params->get('borderwidth', '0');
	$bwidths = preg_split( "/[\s,]+/", $borderwidth );
	for( $i = 0; $i < count($bwidths); ++$i )
	{
		switch( $i )
		{
			case 0:
				$bwidth_top = (int)$bwidths[$i];
				$bwidth_left = $bwidth_top;
				$bwidth_bottom = $bwidth_top;
				$bwidth_right = $bwidth_top;
				break;
			case 1:
				$bwidth_right = (int)$bwidths[$i];
				$bwidth_left = $bwidth_right;
				break;
			case 2:
				$bwidth_bottom = (int)$bwidths[$i];
				break;
			case 3:
				$bwidth_left = (int)$bwidths[$i];
				break;
			default:
				break;
		}
	}
	$bordercolor = $params->get('bordercolor', '0');
	$bcolors = preg_split( "/[\s,]+/", $bordercolor );
	for( $i = 0; $i < count($bcolors); ++$i )
	{
		switch( $i )
		{
			case 0:
				$bcolor_top = $bcolors[$i];
				$bcolor_left = $bcolor_top;
				$bcolor_bottom = $bcolor_top;
				$bcolor_right = $bcolor_top;
				break;
			case 1:
				$bcolor_right = $bcolors[$i];
				$bcolor_left = $bcolor_right;
				break;
			case 2:
				$bcolor_bottom = $bcolors[$i];
				break;
			case 3:
				$bcolor_left = $bcolors[$i];
				break;
			default:
				break;
		}
	}

	if ($params->get('reflectheight') != '')
	{
		$height = JString::str_ireplace('%', '', $params->get('reflectheight'));
		$height = ($height / 100);
	}
	else
	{
		$height = .5;
	}
	$preloadlimit = min( $glidetoimage + $imagestacksize + 2, count($images));
	$opts  = "id: '".$moduleid."', ";
	$opts .= "hideslider: ".$hide_slider.", ";
	$opts .= "hidecaption: ".$hide_caption.", ";
	$opts .= "previousnextbuttons: ".$previousnextbuttons.", ";
	$opts .= "linkfocusedonly: ".$link_focused_only.", ";
	$opts .= "glideto: ".$glidetoimage.", ";
	$opts .= "imagestack: ".$imagestacksize.", ";
	$opts .= "thumbeffects: ".$thumbeffects.", ";
	$opts .= "autostep: ".$autostep.", ";
	$opts .= "noscaling: ".$noscaling.", ";
	$opts .= "xstep: ".$xstep.", ";
	$opts .= "glidespeed: ".$glidespeed.", ";
	$opts .= "reflectionheight: ".$height." ";

	$scrpt  = "// <!--\n";
	$scrpt .= "var ".$moduleid." = new imageFlow( {".$opts."});\n";
	$scrpt .= "window.addEvent( 'domready', function() { ".$moduleid.".setdomready();});\n";

	$hscontrolbar = $params->get('hscontrolbar', 0 );
	if ($hscontrolbar)
	{
		$hsoverlay = JPATH_BASE . DS . 'modules/mod_imageflow/js/hsoverlay.js';
		$hsoverlay = JPath::clean( $hsoverlay, DS );
		if (JFile::exists($hsoverlay))
		{
			$hsdata = JFile::read($hsoverlay);
			$hsdata = preg_replace( "/{moduleid}/i", $moduleid, $hsdata );
			$scrpt .= $hsdata;
		}
	}
	$scrpt .= "// -->\n";
	$modpath = JURI::root(true) . '/modules/mod_imageflow/';
	$document = &JFactory::getDocument();
	$document->addScriptDeclaration( $scrpt );
	JHTML::_('script', 'modules/mod_imageflow/js/imageflow.js', true );
	$cssparam = $params->get('cssfile','modules/mod_imageflow/css/imageflow.css' );
	$cssfile = JURI::root(true).'/'.$cssparam;
	$document->addStyleSheet( $cssfile );

	jimport('joomla.environment.browser');
	$browser	= &JBrowser::getInstance();
	if ($browser->getBrowser() == "msie" && $browser->getMajor() <= 6)
	{
		$ie6css = JPATH_BASE . DS . $cssparam;
		$ie6css = JPath::clean( $ie6css, DS );
		if (JString::substr($ie6css, JString::strlen( $ie6css ) - 4, 4) == '.css')
		{
			$ie6css = JString::substr_replace($ie6css, '-ie6.css', JString::strlen( $ie6css ) - 4, 4 );
		}
		if (JFile::exists($ie6css))
		{
			$styledata = JFile::read($ie6css);
			$newtext = preg_replace( "/src=( )*'( )*([^' ]+)'/i", "src='" . JURI::root(true) . "\\3" . "'", $styledata );
			$document->addStyleDeclaration($newtext);
		}
	}
?>
	<div id="<?php echo $moduleid?>" class="imageflow">
	<div id="<?php echo $moduleid?>_imageflow_loading" class="imageflow_loading">
		<b>Loading images</b><br/>
		<img src="<?php echo $modpath.'images/loading.gif'; ?>" width="208" height="13" alt="loading" />
	</div>
	<div id="<?php echo $moduleid?>_imageflow_images" class="imageflow_images">
<?php
		$base = JPATH_BASE;
		$base = JPath::clean( $base );

	for ($i=0; $i<count($images); $i++)
	{
		$imagename = $images[$i]['img'];
		$rimagename = JPATH_BASE . DS . $images[$i]['img'];
		$rimagename = JPath::clean( $rimagename, DS );
		$rflctimagename = "";
		if (JFile::exists($rimagename))
		{
			$imagename = JPath::clean( JURI::root(true). '/'. $imagename, '/' );

			$height = $params->get('reflectheight');
			if($height == '')
			{
				$height = '50%';
			}
			if (JString::substr($height,-1) != '%')
			{
				$height .= '%';
			}
			if ($height == "0%")
			{
				$rflctimage	= $imagename;
			}
			else
			{
				$rflctparams['height'] = $height;
				$rflctparams['img'] = $rimagename;
				$bgc = $params->get('fadetocolor');
				$bgc = JString::str_ireplace('#', '', $bgc);
				if ($bgc != '')
				{
					$rflctparams['bgc'] = $bgc;
				}
				$tint = $params->get('tintcolor');
				$tint = JString::str_ireplace('#', '', $tint);
				if ($tint != '')
				{
					$rflctparams['tint'] = $tint;
				}
				if ($params->get('reflectresize') != '')
				{
					$resize = $params->get('reflectresize');
					if (JString::substr($resize,-1) != '%')
					{
						$resize .= "%";
					}
					$rflctparams['resize'] = $resize;
				}
				if ($params->get('fadestart') != '')
				{
					$fstart = $params->get('fadestart');
					if (JString::substr($fstart,-1) != '%')
					{
						$fstart .= '%';
					}
					$rflctparams['fade_start'] = $fstart;
				}
				if ($params->get('fadeend') != '')
				{
					$fend = $params->get('fadeend');
					if (JString::substr($fend,-1) != '%')
					{
						$fend .= '%';
					}
					$rflctparams['fade_end'] = $fend;
				}
				if ($params->get('phpmemorylimit') != '')
				{
					$rflctparams['memory_limit'] = $params->get('phpmemorylimit');
				}
				if ($params->get('phpmaxexectime') != '' && is_numeric($params->get('phpmaxexectime')))
				{
					$rflctparams['max_exec_time'] = $params->get('phpmaxexectime');
				}
				if ($blurthumbs > 0)
				{
					$rflctparams['blur'] = $blurthumbs;
				}
				if ($darkenthumbs > 0)
				{
					$rflctparams['darken'] = $darkenthumbs;
				}
				if ($sharpenthumb > 0)
				{
					$rflctparams['sharpen'] = $sharpenthumb;
				}
				$rflctparams['grayscale'] = $grayscalethumbs;
				$rflctparams['transparent'] = $transparent;
				$rflctparams['border_width'] = array( 'top' => $bwidth_top, 'right' => $bwidth_right, 'bottom' => $bwidth_bottom, 'left' => $bwidth_left );
				$rflctparams['border_color'] = array( 'top' => $bcolor_top, 'right' => $bcolor_right, 'bottom' => $bcolor_bottom, 'left' => $bcolor_left );
				$robj = new thumb();
				$rflctimage = $robj->create( $rflctparams );
				if ($rflctimage !== false)
				{
					$rflctimagename = $rflctimage;
					$rflctimage = JPath::clean( $rflctimage );
					if (JString::substr($rflctimage, 0, JString::strlen($base)) == $base)
					{
						$rflctimage = JString::substr( $rflctimage, JString::strlen($base), JString::strlen($rflctimage)-JString::strlen($base) );
					}
					$rflctimage = JPath::clean( JURI::root(true). '/'. $rflctimage, '/' );
				}
				else
				{
					echo "<!-- Failed to generate thumbnail for image " . $rimagename . " -->\n";
				}
			}
			if ($rflctimage !== false)
			{
				$tmpl = $dflttmpl;
				if (isset( $images[$i]['tmplt'] ))
				{
					$tmpltoverride = modImageFlowHelper::getTemplate($images[$i]['tmplt'] );
					if ($tmpltoverride != "")
					{
						$tmpl = $tmpltoverride.PHP_EOL;
					}
				}
				$preload = "<img ";
				if ($i < $preloadlimit)
				{
					$preload .= "class=\"imageflow_preload\" ";
				}
				if ($rflctimagename != "")
				{
					$rflctinfo = getImageSize( $rflctimagename );
					$preload .= "width=\"".$rflctinfo[0]."\" height=\"".$rflctinfo[1]."\" ";
				}
				$tmpl = JString::str_ireplace( "<img ", $preload, $tmpl );
				$def = JString::str_ireplace('{img}', $imagename, $tmpl );
				$def = JString::str_ireplace('{rflct}', $rflctimage, $def  );
				foreach( $images[$i] as $kwd => $val)
				{
					$def = JString::str_ireplace('{'.$kwd.'}', $val, $def);
				}
				while ( preg_match( "/[{]([A-Za-z0-9]*)[}]/i", $def, $match ) > 0)
				{
					$def = str_replace( $match[0], '', $def);
				}
				echo $def;
			}
		}
		else
		{
			echo "<!-- \n";
			echo "Unable to find image " . $rimagename . " for gallery display.\n";
			echo "    Image:    ".$images[$i]['img']." \n";
			echo "    Pathfile: ".$images[$i]['pathfile']." \n";
			echo "    Pathbase: ".$images[$i]['pathbase']." \n";
			echo " -->\n";
		}
	}
?>
	</div>
<?php
if ($hide_caption == 'false')
{
?>
	<div id="<?php echo $moduleid?>_imageflow_captions" class="imageflow_captions"></div>
<?php
}
?>
<?php
	if ($autostep > 0 || $previousnextbuttons == 'true' || $hide_slider == 'false')
	{
?>
	<div id="<?php echo $moduleid?>_imageflow_navbuttons" class="imageflow_navbuttons"></div>
<?php
	}
?>
</div>